from src.choice_api.utils import timestamp2data_str

def parse_attendance(data, columns_lst):
    if data is None or "list" not in data:
        return None

    data_lst = data["list"]
    if not data_lst:
        data_lst = []

    # 真实要获取的字段
    columns_lst = ["user","checkType","timeResult","workDate"]
    res_table_columns = ["人员","打卡类型","打卡结果","时间"]

    timeResult_mapping = {
        'Normal': '已打卡',
        'NotSigned': '未打卡',
        'Early': '早退',
        'Late': '迟到',
        'SeriousLate': '严重迟到',
        'Absenteeism': '旷工迟到',
        'Rest': '休息'
    }


    res_table = []

    for row in data_lst:
        row_lst = []
        for column in columns_lst:
            if column == "user":
                row_lst.append(row["user"]["name"])
            elif column == "checkType":
                if row["checkType"] == "OffDuty":
                    row_lst.append("下班打卡")
                elif row["checkType"] == "OnDuty":
                    row_lst.append("上班打卡")
                else:
                    row_lst.append(row["checkType"])
            elif column == "timeResult":
                if row["timeResult"] in timeResult_mapping:
                    row_lst.append(timeResult_mapping[row["timeResult"]])
                else:
                    row_lst.append(row["timeResult"])
            elif column == "workDate":
                row_lst.append(timestamp2data_str(row["workDate"]/1000))
        res_table.append(row_lst)


    return {
        "res_table_title": "查询【员工考勤】相关数据如下：",
        "res_table_columns": res_table_columns,
        "res_table": res_table
    }